package com.saclub.app.handler;

import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.offroader.utils.JsonUtils;
import com.offroader.utils.OnlineParamUtils;
import com.offroader.utils.PhoneUtils;
import com.offroader.utils.StringUtils;
import com.saclub.app.R;
import com.saclub.app.bean.page.NewsListPage;
import com.saclub.app.bean.part.News;
import com.saclub.app.common.Constants;
import com.saclub.app.common.MyApp;
import com.saclub.app.common.MyPageHelper;
import com.saclub.app.fragment.WelcomeFragment;
import com.saclub.app.utils.ImageUtils;

public class WelcomeAdResponseHandler extends MyBaseHttpResponseHandler<WelcomeFragment, NewsListPage> {

	@Override
	public void deal() {

		List<News> newsList = page.getNewslist();
		final News news = newsList.iterator().next();

		ImageView adImage = caller.getAdImageView();
		adImage.setImageResource(R.drawable.def);

		//加载广告
		ImageUtils.load(adImage, news.getNewImage(), new ImageLoadingListener() {

			@Override
			public void onLoadingStarted(String arg0, View arg1) {

			}

			@Override
			public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {

			}

			@Override
			public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {

				String welcomAdVcode = OnlineParamUtils.get(Constants.ONLINE_PARAMS_KEY_WELCOM_AD_VCODE);

				if (StringUtils.isNotBlank(welcomAdVcode)) {

					int vCode = Integer.parseInt(welcomAdVcode);
					int appCode = PhoneUtils.getAppVersionCode(MyApp.getInstance());
					
					/**
					 * 小于等于在线参数版本号显示否则不显示
					 */
					if (appCode <= vCode)
						caller.getAdImageViewLayout().setVisibility(View.VISIBLE);
					else
						caller.getAdImageViewLayout().setVisibility(View.GONE);

				} else {
					caller.getAdImageViewLayout().setVisibility(View.GONE);
				}

			}

			@Override
			public void onLoadingCancelled(String arg0, View arg1) {

			}
		});

		adImage.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				String url = news.getNewUrl();
				String title = news.getNewTitle();

				//非法不处理
				if (StringUtils.isBlank(url))
					return;

				Bundle args = new Bundle();
				args.putString("news", JsonUtils.toJson(news));

				MyPageHelper.webView.showMyWebViewPage(caller.getActivity(), url, title, args);

			}
		});

	}

}
